#include "myopenglwidget.h"
#include<QTime>
unsigned int VBO,VAO,EBO;
QMatrix4x4 projection,view,model;
float vertices[]={
    //正方体的坐标
    -0.5f,-0.5f,-0.5f,0.0f,0.0f,
    0.5f,-0.5f,-0.5f,1.0f,0.0f,
    0.5f,0.5f,-0.5f,1.0f,1.0f,
    0.5f,0.5f,-0.5f,1.0f,1.0f,
    -0.5f,0.5f,-0.5f,0.0f,1.0f,
    -0.5f,-0.5f,-0.5f,0.0f,0.0f,

    -0.5f,-0.5f,0.5f,0.0f,0.0f,
    0.5f,-0.5f,0.5f,1.0f,0.0f,
    0.5f,0.5f,0.5f,1.0f,1.0f,
    0.5f,0.5f,0.5f,1.0f,1.0f,
    -0.5f,0.5f,0.5f,0.0f,1.0f,
    -0.5f,-0.5f,0.5f,0.0f,0.0f,

    -0.5f,0.5f,0.5f,1.0f,0.0f,
    -0.5f,0.5f,-0.5f,1.0f,1.0f,
    -0.5f,-0.5f,-0.5f,0.0f,1.0f,
    -0.5f,-0.5f,-0.5f,0.0f,1.0f,
    -0.5f,-0.5f,0.5f,0.0f,0.0f,
    -0.5f,0.5f,0.5f,1.0f,0.0f,

    0.5f,0.5f,0.5f,1.0f,0.0f,
    0.5f,0.5f,-0.5f,1.0f,1.0f,
    0.5f,-0.5f,-0.5f,0.0f,1.0f,
    0.5f,-0.5f,-0.5f,0.0f,1.0f,
    0.5f,-0.5f,0.5f,0.0f,0.0f,
    0.5f,0.5f,0.5f,1.0f,0.0f,

    -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
    0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
    0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
    0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
    -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
    -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,

    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
    0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
    0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
    0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
    -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
};
QVector<QVector3D> cubePositions={
    QVector3D(0.0f,0.0f,0.0f),
    QVector3D(2.0f,5.0f,-15.0f),
    QVector3D(-1.5f,-2.2f,-2.5f),
    QVector3D(-3.8f,-2.0f,-12.3f),
    QVector3D(2.4f,-0.4f,-3.5f),
    QVector3D(-1.7f,3.0f,-7.5f),
    QVector3D(1.3f,-2.0f,-2.5f),
    QVector3D(1.5f,2.0f,-2.5f),
    QVector3D(1.5f,0.2f,-1.5f),
    QVector3D(-1.3f,1.0f,-1.5f),
};

unsigned int indices[]={
    0,1,3,      //第一个三角形
    1,2,3       //第二个三角形
};


MyOpenglWidget::MyOpenglWidget(QWidget *parent):QOpenGLWidget(parent) {
    setFocusPolicy(Qt::StrongFocus);
    timer.setInterval(200);
    connect(&timer,&QTimer::timeout,[this](){
        update();
    });

    timer.start();
}

MyOpenglWidget::~MyOpenglWidget()
{
    delete textureWall;
    makeCurrent();
    glDeleteVertexArrays(1,&VAO);
    glDeleteBuffers(1,&VBO);
    glDeleteBuffers(1,&EBO);

    doneCurrent();
}

void MyOpenglWidget::drawRectangle()
{
    ShapeType=GL_TRIANGLES;
    isDraw=true;
    update();
}

void MyOpenglWidget::clear()
{
    isDraw=false;
    update();
}

void MyOpenglWidget::setPolygonMode(bool isWire)
{
    makeCurrent();
    if(isWire)
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    else
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    doneCurrent();
    update();
}

void MyOpenglWidget::initializeGL()
{
    this->initializeOpenGLFunctions();
     glEnable(GL_DEPTH_TEST);
    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);

    //将数据传入显存
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    //告知显卡如何解析
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(float),(GLvoid*)0);
    //glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,5*sizeof(float),(GLvoid*)(3*sizeof(float)));
    glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5*sizeof(float),(GLvoid*)(3*sizeof(float)));

    //开启VAO的属性
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    //glEnableVertexAttribArray(2);

    //EBO
    glGenBuffers(1,&EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);

    //链接 link shaders
    shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shaders/shapes.vert");
    shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shaders/shapes.frag");

    bool success=shaderProgram.link();
    if(!success)
        qDebug()<<shaderProgram.log();
    shaderProgram.bind();

    textureWall=new QOpenGLTexture(QImage(":/resources/container.jpg").mirrored());
    textureContainer=new QOpenGLTexture(QImage(":/resources/awesomeface.png").mirrored());

    textureContainer->generateMipMaps();
    shaderProgram.setUniformValue("textureWall",0);
    shaderProgram.setUniformValue("textureContainer",1);
    //解绑
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,0);

    projection.perspective(80,(float)width()/(1.5*height()),0.1f,100.0f);
    shaderProgram.setUniformValue("projection",projection);

    view.translate(0.0f,0.0f,-3.0f);
    shaderProgram.setUniformValue("view",view);
}

void MyOpenglWidget::resizeGL(int w, int h)
{

}

void MyOpenglWidget::paintGL()
{
    glClearColor(0.2f,0.3f,0.3f,1.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(!isDraw)
        return;
    shaderProgram.bind();

    glBindVertexArray(VAO);
    textureWall->bind(0);
    textureContainer->bind(1);
    int timeValue=QTime::currentTime().msec();
    int i=0;
    foreach (auto& item,cubePositions) {
        ++i;
        model.setToIdentity();
        model.translate(item);
        if(i==1 || i%3==0)
            model.rotate(timeValue,1.0f,0.3f,0.5f);
        shaderProgram.setUniformValue("model",model);
        //glDrawElements(ShapeType,6,GL_UNSIGNED_INT,0);

        glDrawArrays(ShapeType,0,36);
    }


    //解绑
    glBindVertexArray(0);
}

